package com.dingwen.treasure.open.api.sms.aliyun.service;

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.dingwen.treasure.open.api.sms.aliyun.bo.AliSmsSendBo;
import com.dingwen.treasure.open.api.sms.aliyun.config.AliyunSmsConfig;
import com.dingwen.treasure.open.api.sms.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 阿里巴巴短信服务impl
 *
 * @author dingwen
 * @date 2022/12/09
 */
@Component("aliyunSmsService")
@Slf4j
public class AliyunSmsServiceImpl implements SmsService<AliSmsSendBo, String, Object> {

    @Resource
    private AliyunSmsConfig aliyunSmsConfig;

    @Override
    public boolean send(AliSmsSendBo aliSmsSendBo) {

        try {
            // 构建短信发送请求
            SendSmsRequest request = new SendSmsRequest()
                    // 电话号码
                    .setPhoneNumbers(aliSmsSendBo.getPhoneNumber())
                    // 短信签名
                    .setSignName(aliSmsSendBo.getSingName())
                    // 短信模版code
                    .setTemplateCode(aliSmsSendBo.getTemplateCode())
                    // 短信模版参数
                    .setTemplateParam(aliSmsSendBo.getTemplateParam());
            // 发送短信
            SendSmsResponse response = aliyunSmsConfig.aliSmsClient().sendSms(request);
            log.info("阿里云短信发送测试响应结果:{}", JSONUtil.toJsonPrettyStr(response));
            return Boolean.TRUE;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void sendBatch(List<AliSmsSendBo> aliSmsSendBos) {
        //TODO 批量发送
    }

    @Override
    public Object queryDetail(String s) {
        // TODO 查询详情
        return null;
    }

    @Override
    public Object queryStatistics(Object o) {
        // TODO 查询统计、入参、返回值等
        return null;
    }
}
